package com.password.desktop.UI;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

import com.password.desktop.util.SysUtil;

public class InputForm extends JPanel {

	private Border focusBorder = new MotifBorders.FocusBorder(new Color(204, 204, 204), new Color(102, 175, 233));

	private JTextField textFile;
	private final JLabel label;
	private static final long serialVersionUID = 1L;

	public InputForm(String title) {
		this(title, JTextField.class);
	}

	public InputForm(String title, Class<? extends JTextField> clazz) {
		label = new JLabel(title);
		label.setFont(SysUtil.FONT_DEFAULT);
		label.setAlignmentX(JLabel.CENTER_ALIGNMENT);
		label.setVerticalAlignment(JLabel.CENTER);
		label.setHorizontalAlignment(JLabel.RIGHT);
		label.setPreferredSize(new Dimension(80, 32));
		try {
			textFile = clazz.newInstance();
		} catch (Exception e) {
			textFile = new JTextField();
		}
		textFile.setPreferredSize(new Dimension(180, 32));
		textFile.setBorder(focusBorder);
		textFile.addFocusListener(new FocusListener() {

			@Override
			public void focusLost(FocusEvent e) {
				repaint();
			}

			@Override
			public void focusGained(FocusEvent e) {
				repaint();
			}
		});

		initSet();
	}

	public String getText() {
		return textFile.getText();
	}

	public void initSet() {
		setLayout(new FlowLayout(FlowLayout.LEFT, 16, 8));
		setOpaque(false);
		add(label);
		add(textFile);
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.setSize(420, 320);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		InputForm inputForm = new InputForm("sdfsdf");
		inputForm.setBounds(0, 0, 230, 64);
		frame.getContentPane().add(inputForm);
		frame.setVisible(true);
	}

}
